# -*- coding: utf-8 -*-

try:
    import pymel.core as pm

except ImportError:
    import maya.cmds as cmds
    import subprocess
    import sys
    
    print('Pymel not found, installing...')
    maya_python = sys.executable
    try:
        subprocess.check_call([maya_python, '-m', 'pip', 'install', 'pymel'])
        import pymel.core as pm
        print('Pymel installed successfully')
    except Exception as e:
        cmds.warning('Failed to install pymel: {}'.format(e))
        raise

import core

print('L_Link')

def add_L_link_menu():
    main_window = pm.language.melGlobals['gMainWindow']
    menu_obj = 'L_Link'
    menu_label = 'L_Link'
    
    try:
        if pm.menu(menu_obj, exists=True, parent=main_window):
            pm.deleteUI(menu_obj)  
    except Exception as e:
        print(e)

    L_link_menu = pm.menu(menu_obj, label=menu_label, parent=main_window, tearOff=True)
    
  
    pm.menuItem(label='Send to Blender', command='core.send_to_blender()', parent=L_link_menu)
    
    return menu_label

if not pm.about(batch=True):
    # 使用evalDeferred延迟执行，并添加延迟时间参数(毫秒)
    pm.general.evalDeferred(add_L_link_menu, lowestPriority=True)
    
    # 将文件监听器启动也延迟执行，避免同时初始化
    pm.general.evalDeferred(core.start_file_listener, lowestPriority=True)